package com.sloera.config;

import android.content.Context;
import android.os.Environment;
import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.flattener.ClassicFlattener;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.Printer;
import com.elvishew.xlog.printer.file.FilePrinter;
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy;
import com.sloera.formater.FileNameGenerator;
import com.sloera.interceptor.XlogInterceptor;
import com.sloera.trime.R;

import java.io.File;

public class XlogConfig {
    /**
     * Initialize XLog.
     */
    public static void initXlog(Context applicationContext, int logLevel) {
        LogConfiguration config = new LogConfiguration.Builder()
                //            .logLevel(BuildConfig.DEBUG ? LogLevel.ALL : LogLevel.INFO)       // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                .logLevel(logLevel)       // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                .tag(applicationContext.getString(R.string.global_tag))                   // Specify TAG, default: "X-LOG"
                // .t()                                                   // 允许打印线程信息，默认禁止
                // .st(2)                                                 // 允许打印深度为2的调用栈信息，默认禁止
                // .b()                                             // Enable border, disabled by default
                // .jsonFormatter(new MyJsonFormatter())               // Default: DefaultJsonFormatter
                // .xmlFormatter(new MyXmlFormatter())                 // Default: DefaultXmlFormatter
                // .throwableFormatter(new MyThrowableFormatter())     // Default: DefaultThrowableFormatter
                // .threadFormatter(new MyThreadFormatter())           // Default: DefaultThreadFormatter
                // .stackTraceFormatter(new MyStackTraceFormatter())   // Default: DefaultStackTraceFormatter
                // .borderFormatter(new MyBoardFormatter())            // Default: DefaultBorderFormatter
                // .addObjectFormatter(AnyClass.class,                 // Add formatter for specific class of object
                //     new AnyClassObjectFormatter())                  // Use Object.toString() by default
                .addInterceptor(XlogInterceptor.getInstance())
                // .addInterceptor(new WhitelistTagsFilterInterceptor( // Add whitelist tags filter
                //     "whitelist1", "whitelist2", "whitelist3"))
                // .addInterceptor(new MyInterceptor())                // Add a log interceptor
                .build();

        Printer androidPrinter = new AndroidPrinter();             // Printer that print the log using android.util.Log
        Printer filePrinter = new FilePrinter                      // Printer that print the log to the file system
                .Builder(new File(Environment.getExternalStorageDirectory(), applicationContext.getString(R.string.log_file)).getPath())       // Specify the path to save log file
                .fileNameGenerator(new FileNameGenerator())        // Default: ChangelessFileNameGenerator("log")
                // .backupStrategy(new MyBackupStrategy())             // Default: FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy(new FileLastModifiedCleanStrategy(1000 * 60 * 60 * 24 * 7))     // Default: NeverCleanStrategy()
                .flattener(new ClassicFlattener())                     // Default: DefaultFlattener
                .build();

        XLog.init(                                                 // Initialize XLog
                config,                                                // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
                androidPrinter,                                        // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
                filePrinter);

        // For future usage: partial usage in MainActivity.
        //    globalFilePrinter = filePrinter;
    }
}
